package com.atguigu.order.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.order.entity.TOrder;
import com.atguigu.order.service.TOrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-08-18
 */
@RestController
@RequestMapping("/order/torder")
//@CrossOrigin
public class TOrderController {

    @Autowired
    TOrderService tOrderService;

    //生成订单的方法
    @PostMapping("createOrder/{courseId}")
    public R createOrder(@PathVariable String courseId, HttpServletRequest request){
        String orderNo=tOrderService.createOrder(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return R.ok().data("orderNo",orderNo);
    }

    //根据订单id查询订单信息
    @GetMapping("getOrderId/{id}")
    public R getOrderId(@PathVariable String id){
        QueryWrapper wrapper = new QueryWrapper();
        System.out.println("订单号"+id);
        wrapper.eq("order_no",id);
        TOrder order = tOrderService.getOne(wrapper);
        System.out.println("order"+order);
        return R.ok().data("order",order);
    }

    //根据课程id和用户id查询订单中订单中的状态
    @GetMapping("isBuyCourse/{courseId}/{memberId}")
    public Boolean isButCOurse(@PathVariable String courseId,@PathVariable String memberId){
        QueryWrapper<TOrder> wrapper = new QueryWrapper();
        wrapper.eq("course_id",courseId);
        wrapper.eq("member_id",memberId);
        wrapper.eq("status",1);
        int count = tOrderService.count(wrapper);
        if(count>0){
            return true;
        }else{
            return false;
        }
    }


}

